import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_study/model/category_model.dart';
import 'package:flutter_study/page/rank_widget_item.dart';
import 'package:flutter_study/provider/category_detail_model.dart';
import 'package:flutter_study/util/navigator_manager.dart';
import 'package:flutter_study/widget/loading_container.dart';
import 'package:flutter_study/widget/provider_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class CategoryDetailPage extends StatefulWidget {
  final CategoryModel categoryModel;

  const CategoryDetailPage({Key key, @required this.categoryModel})
      : super(key: key);

  @override
  _CategoryDetailPageState createState() => _CategoryDetailPageState();
}

class _CategoryDetailPageState extends State<CategoryDetailPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: ProviderWidget<CategoryDetailModel>(
        model: CategoryDetailModel(widget.categoryModel.id),
        onModelInit: (model) {
          model.loadMore(loadMore: false);
        },
        builder: (context, model, child) {
          return NestedScrollView(
              headerSliverBuilder: (context, innerBoxIsScrolled) {
                return [
                  SliverAppBar(
                    elevation: 0,
                    brightness: Brightness.light,
                    backgroundColor: Colors.white,
                    expandedHeight: 200,
                    pinned: true,
                    flexibleSpace: LayoutBuilder(
                      builder: (context, constraints) {
                        model.changeExpendStatusByOffset(
                            (MediaQuery.of(context).padding.top).toInt() + 56,
                            constraints.biggest.height.toInt());
                        return FlexibleSpaceBar(
                          centerTitle: false,
                          background: CachedNetworkImage(
                            imageUrl: widget.categoryModel.headerImage,
                            fit: BoxFit.cover,
                          ),
                          title: Text(
                            widget.categoryModel.name,
                            style: TextStyle(
                                color:
                                    model.expend ? Colors.white : Colors.black),
                          ),
                        );
                      },
                    ),
                    leading: GestureDetector(
                      onTap: () {
                        NavigatorManager.back();
                      },
                      child: Icon(
                        Icons.arrow_back,
                        color: Colors.black,
                      ),
                    ),
                  )
                ];
              },
              body: buildBody(model));
        },
      ),
    );
  }

  Widget buildBody(CategoryDetailModel model) {
    return LoadingContainer(
        error: model.error,
        retry: model.retry,
        loading: model.loading,
        child: SmartRefresher(
            controller: model.refreshController,
            onLoading: model.loadMore,
            enablePullDown: false,
            enablePullUp: true,
            child: ListView.builder(
              itemCount: model.itemList.length,
              itemBuilder: (context, index) {
                print('打印item$index');
                return RankWidgetItem(
                  item: model.itemList[index],
                  showCategory: false,
                  showDivider: false,
                );
              },
            )));
  }
}
